Make decayEffect a continuous function of sample timestamp#33
Open
ps2 wants to merge 1 commit into
Open
Conversation
Reformulates decayEffect using a closed-form quadratic in time-since-sample rather than accumulating step-by-step from the floored simulation boundary. This makes the effect value at any future absolute timestamp independent of which delta-sized simulation bucket the sample's startDate falls into. For samples aligned to delta boundaries the two formulations are mathematically identical. For unaligned samples (the common case with real CGM streams) the new formulation removes a small discontinuity that the old code exhibited at bucket boundaries. Adds LoopMathTests covering continuity across a delta boundary. Existing fixture-calibrated tests are re-pinned to the new values; per-prediction drift is on the order of 0.1 mg/dL. Ports the LoopMath change from LoopKit/LoopKit#556 by Moti Nisenson-Ken to the LoopAlgorithm package, where decayEffect now lives.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GlucoseValue.decayEffectcurrently accumulates the decay step-by-step starting fromstartDate.dateFlooredToTimeInterval(delta). That makes the effect value at any future absolute timestamp depend on which delta-sized simulation bucket the sample'sstartDatehappens to fall into — so two CGM samples a fraction of a minute apart can produce noticeably different effect curves at the same future point, even though the math is supposed to be smooth.This PR reformulates
decayEffectas a closed-form quadraticf(t) = a*t² + b*t + cwithtmeasured from the actual sample timestamp. For samples aligned to delta boundaries the two formulations are mathematically equivalent; for unaligned samples the new formulation removes the bucket-boundary discontinuity.This is a port of LoopKit/LoopKit#556 by Moti Nisenson-Ken, brought over to the LoopAlgorithm package where
decayEffectnow lives.Changes
Sources/LoopAlgorithm/LoopMath.swift: reformulated decay computation.Tests/LoopAlgorithmTests/LoopMathTests.swift: newtestDecayEffectIsContinuousAcrossSimulationBoundaryexercising the continuity property.